package org.jeecg.modules.demo.evaluate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.evaluate.entity.Evaluate;
import org.jeecg.modules.demo.evaluate.entity.TraderE;
import org.jeecg.modules.demo.evaluate.service.IEvaluateService;
import org.jeecg.modules.demo.evaluate.service.ITraderEService;
import org.jeecg.modules.demo.mthetkz.entity.MtHetkz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 评价
 */
@Api(tags="Evaluate")
@RestController
@RequestMapping("/Evaluate/Evaluate")
@Slf4j
public class EvaluateController {

    @Autowired
    private IEvaluateService evaluateService;

    @Autowired
    private ITraderEService traderEService;

    /**
     * 分页列表查询 未审核
     *
     * @param traderE
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "Evaluate-分页列表查询 未审核")
    @ApiOperation(value="Evaluate-分页列表查询 未审核", notes="Evaluate-分页列表查询 未审核")
    @GetMapping(value = "/list")
    public Result<IPage<TraderE>> queryPageList(TraderE traderE,
                                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                HttpServletRequest req) {

        QueryWrapper<TraderE> queryWrapper = QueryGenerator.initQueryWrapper(traderE, req.getParameterMap());

        queryWrapper.eq("INOUTSIDE", 2);
        Page<TraderE> page = new Page<TraderE>(pageNo, pageSize);
        List<TraderE> list = traderEService.list(page, queryWrapper);

        for(TraderE te: list){
            System.out.println(te);
            String score =evaluateService.getTotalscoreBytraderid(te.getTraderid());
            System.out.println("score: "+score);

            if("".equals( score) || score == null){
                te.setTotalscore(-1);
            }else {
                te.setTotalscore(Double.valueOf(score));
            }
        }
        IPage<TraderE> pageList = page.setRecords(list);
        return Result.OK(pageList);

    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "Evaluate-通过id查询")
    @ApiOperation(value="Evaluate-通过id查询", notes="Evaluate-通过id查询")
    @GetMapping(value = "/queryByid")
    public Result<Evaluate> queryByid(@RequestParam(name="id",required=true) String id) {
        Evaluate evaluate = evaluateService.getById(id);
        if(evaluate==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(evaluate);
    }

    /**
     * 通过公司id查询
     *
     * @param traderid
     * @return
     */
    //@AutoLog(value = "Evaluate-通过id查询")
    @ApiOperation(value="Evaluate-通过id查询", notes="Evaluate-通过id查询")
    @GetMapping(value = "/queryByTraderid")
    public Result<Evaluate> queryByTraderid(@RequestParam(name="traderid",required=true) Integer traderid) {
//        Evaluate evaluate = evaluateService.getById(traderid);
        Evaluate evaluate = evaluateService.getByTraderid(traderid);
        if(evaluate==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(evaluate);
    }

    /**
     *   添加
     *
     * @param evaluate
     * @return
     */
    @AutoLog(value = "Evaluate-添加")
    @ApiOperation(value="Evaluate-添加", notes="Evaluate-添加")
//    @RequiresPermissions("Evaluate:Evaluate:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Evaluate evaluate) {
//        double totalscore = (evaluate.getWgzl() + evaluate.getWxjsx() + evaluate.getXnwdx())
//                        + (evaluate.getXysd() + evaluate.getWtjjl() + evaluate.getKhmyd())
//                        + (evaluate.getJfzsl() + evaluate.getKczzl() + evaluate.getSjzql())
//                        + (evaluate.getHtlxqk() + evaluate.getWwgjl() + evaluate.getFyzfjsx());
//        evaluate.setTotalscore(totalscore);
        evaluateService.save(evaluate);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param evaluate
     * @return
     */
    @AutoLog(value = "mt_hetkz-编辑")
    @ApiOperation(value="mt_hetkz-编辑", notes="mt_hetkz-编辑")
//    @RequiresPermissions("Evaluate:Evaluate:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody Evaluate evaluate) {
//        double totalscore = (evaluate.getWgzl() + evaluate.getWxjsx() + evaluate.getXnwdx())
//                + (evaluate.getXysd() + evaluate.getWtjjl() + evaluate.getKhmyd())
//                + (evaluate.getJfzsl() + evaluate.getKczzl() + evaluate.getSjzql())
//                + (evaluate.getHtlxqk() + evaluate.getWwgjl() + evaluate.getFyzfjsx());
//        evaluate.setTotalscore(totalscore);
        evaluateService.updateById(evaluate);
        return Result.OK("编辑成功!");
    }



}
